package
{

	import flash.display.MovieClip;


public dynamic class Game extends MovieClip
{
    var PiRads: Number = 0.0174532925199;
    var powerUps = ["Tea Bags", "Clotted Cream", "Bitter", "Shakespeare", "Cabbie", "Bobby", "Phone Box", "Rain"];
    var hitSounds = ["bloodyhellmate", "bloodyhell", "crikey", "struth", "damn", "ow0", "ow1", "ow2", "ow3", "ow4", "ow5", "ow0", "ow1", "ow2", "ow3", "ow4", "ow5", "crikey2"];
	/*
    var _parent;
    var _xmouse;
    var _ymouse;
	*/
    var ashEnergy;
    var ashes;
    var ashesMC;
//    var attachMovie;
    var batCount;
    var cPoses;
    var cash;
    var count;
    var createEmptyMovieClip;
    var cursor;
    var cursorBusy;
    var cursorFrame;
    var delCount;
    var effectsMC;
    var enemies;
    var enemiesDone;
    var enemyMCount;
    var enemyMIncr;
    var enemyMaxInt;
    var enemyMinInt;
    var flyingEnemies;
    var ground;
    var interLevel;
    var level;
    var maxAshEnergy;
    var num;
    var objectsA;
    var onEnterFrame;
    var paused;
    var pointsA;
    var powerUpsAmounts;
    var prevSound;
    var score;
    var shakespeare;
    var spritesMC;
    var theInterface;
    var weaponNum;
    var weaponsCount;

    function Game()
    {
        super();
    }

    function init()
    {
        SoundManager.stopSound("cockneyBG");
        SoundManager.volSet("bgloop", 0);
        SoundManager.startSound("bgloop", 0, 10000);
        SoundManager.fadeSound("bgloop", 70, 40);
        this.pointsA = [];
        this.cPoses = [];
        var __reg2 = 0;
        while (__reg2 < 2) 
        {
            this.cPoses.push(new Point(this.mouseX, this.mouseY));
            ++__reg2;
        }
        var __reg3 = this.attachMovie("pathMap", "pd", 0);
        __reg2 = 0;
        while (__reg2 < __reg3._totalframes) 
        {
            __reg3.gotoAndStop(__reg2 + 1);
            this.pointsA[__reg2] = new Point(__reg3.mc._x, __reg3.mc._y);
            ++__reg2;
        }
        __reg3.removeMovieClip();
        this.score = this.cash = 0;
        this.cash = 100;
        this.ashEnergy = this.maxAshEnergy = 100;
        this.cursor = this.parent.theCursor;
        this.theInterface = this.attachMovie("interface", "int", 98);
        this.makeWeaponBtns();
        this.powerUpsAmounts = [1, 1, 1, 1, 1, 1, 1, 1];
        this.parent.skyBar.pause.gotoAndPlay("in");
        this.parent.skyBar.pause.pause.gotoAndStop(1);
    }

    function makeWeaponBtns()
    {
        var __reg2 = 0;
        while (__reg2 < this.powerUps.length) 
        {
            var __reg3 = this.theInterface["btn" + __reg2];
            __reg3.icons.gotoAndStop(__reg2 + 1);
            __reg3.btn.num = __reg2;
            ++__reg2;
        }
        this.refreshWeaponBtns();
    }

    function refreshWeaponBtns()
    {
        var __reg3 = 0;
        for (;;) 
        {
            if (__reg3 >= this.powerUps.length) 
            {
                return;
            }
            var __reg2 = this.theInterface["btn" + __reg3];
            __reg2.amount.text = this.powerUpsAmounts[__reg3];
            __reg2.amount2.text = this.powerUpsAmounts[__reg3];
            if (this.powerUpsAmounts[__reg3] > 0) 
            {
                __reg2.btn._alpha = __reg2.icons._alpha = 100;
                __reg2.btn.onRelease = function ()
                {
                    this.parent.parent.parent.selectWeapon(this.num);
                }
                ;
                __reg2.btn.onRollOver = function ()
                {
                    this.parent.parent.weaponName.text = this.parent.parent.parent.powerUps[this.num];
                }
                ;
            }
            else 
            {
                __reg2.btn._alpha = __reg2.icons._alpha = 50;
                delete __reg2.btn.onRelease;
                delete __reg2.btn.onRollOver;
            }
            ++__reg3;
        }
    }

    function startLevel(tLevel)
    {
        this.level = tLevel;
        this.enemiesDone = 15 + 8 * this.level;
        this.enemyMaxInt = 60;
        this.enemyMinInt = 30 - (this.level - 1) * 4;
        this.enemyMIncr = (this.enemyMaxInt - this.enemyMinInt - 1) / this.enemiesDone;
        if (this.enemyMinInt < 10) 
        {
            this.enemyMinInt = 10;
        }
        this.enemies = [];
        this.flyingEnemies = [];
        this.objectsA = [];
        this.prevSound = [-1, -1, -1];
        this.spritesMC = this.createEmptyMovieClip("smc", 2);
        this.effectsMC = this.createEmptyMovieClip("emc", 3);
        Mouse.hide();
        this.cursorBusy = false;
        this.cursorFrame = "def";
        this.makeEnemy(1, 2);
        this.count = this.enemyMCount = this.weaponsCount = 0;
        this.showAshEnergy();
        this.refreshWeaponBtns();
        this.paused = false;
        this.parent.skyBar.pause.pause.btn.onRelease = function ()
        {
            this.parent.parent.parent.parent.theGame.pauseToggle();
        }
        ;
        this.updateScore();
        this.onEnterFrame = this.EF;
    }

    function pauseToggle()
    {
        this.paused = !this.paused;
        if (this.paused) 
        {
            delete this.onEnterFrame;
            this.parent.skyBar.pause.pause.gotoAndStop(2);
            this.cursor.changeState("def");
            return;
        }
        this.onEnterFrame = this.EF;
        this.parent.skyBar.pause.pause.gotoAndStop(1);
        this.cursor.changeState(this.cursorFrame);
    }

    function soundCleanup()
    {
        SoundManager.stopSound("rain");
        SoundManager.stopSound("snore");
    }

    function finishLevel()
    {
        this.soundCleanup();
        delete this.onEnterFrame;
        var __reg2 = this.attachMovie("interLevel", "interLevel", 100);
        __reg2.txt.gotoAndStop(this.level);
        this.paused = true;
        delete this.parent.skyBar.pause.pause.btn.onRelease;
        this.cursor.changeState("def");
    }

    function giveLevelBonus()
    {
        this.score = this.score + this.level * 10;
        this.updateScore();
    }

    function getWeapon()
    {
        this.paused;
        var __reg2 = Math.floor(Math.random() * this.powerUpsAmounts.length);
        ++this.powerUpsAmounts[__reg2];
        this.theInterface["btn" + __reg2].flash.gotoAndPlay(2);
        this.refreshWeaponBtns();
    }

    function nextLevel()
    {
        this.interLevel.removeMovieClip();
        this.startLevel(++this.level);
    }

    function makeEnemy(tSide, tLevel)
    {
        if (this.enemiesDone > 0 || this.level == 5) 
        {
            var __reg2 = this.spritesMC.getNextHighestDepth();
            var __reg3 = this.spritesMC.attachMovie("man", "man" + __reg2, __reg2);
            __reg3.init(tSide, tLevel);
            this.enemies[__reg2] = __reg3;
            --this.enemiesDone;
        }
    }

    function makeWeaponCrate()
    {
        var __reg2 = this.spritesMC.getNextHighestDepth();
        var __reg3 = this.spritesMC.attachMovie("weaponCrate", "wCrate" + __reg2, __reg2);
        this.objectsA[__reg2] = __reg3;
        __reg3.init();
    }

    function EF()
    {
        for (var __reg3 in this.enemies) 
        {
            this.enemies[__reg3].EF();
        }
        for (__reg3 in this.objectsA) 
        {
            this.objectsA[__reg3].EF();
        }
        var __reg4 = this.count - this.enemyMCount;
        if (Math.random() > 0.9 || __reg4 > this.enemyMaxInt) 
        {
            if (__reg4 > this.enemyMinInt) 
            {
                var __reg5 = 1;
                if (Math.random() > 0.5) 
                {
                    __reg5 = __reg5 * -1;
                }
                this.makeEnemy(__reg5, this.level + this.count / 400);
                this.enemyMCount = this.count;
                this.enemyMaxInt = this.enemyMaxInt - this.enemyMIncr;
                if (this.enemyMaxInt < this.enemyMinInt) 
                {
                    this.enemyMaxInt = this.enemyMinInt;
                }
            }
        }
        if (this.getALength(this.flyingEnemies) > 0 && this.cursor._y < 250 && !this.cursorBusy && this.batCount-- < 0) 
        {
            this.cursor.changeState("cricketBat");
        }
        else 
        {
            this.cursor.changeState(this.cursorFrame);
        }
        this.cPoses.shift();
        this.cPoses.push(new Point(this.cursor._x, this.cursor._y));
        if (this.weaponNum >= 0) 
        {
            if (this.delCount-- < 0) 
            {
                if (this.weaponNum === 3) 
                {
                    for (__reg3 in this.enemies) 
                    {
                        var __reg2 = this.enemies[__reg3];
                        if (__reg2.onLine && __reg2.defFrame == "shakespear") 
                        {
                            __reg2.stopShakespear();
                        }
                    }
                    this.shakespeare.gotoAndPlay("out");
                    SoundManager.stopSound("snore");
                }
                this.weaponNum = -1;
            }
        }
        if (Math.random() > 0.99 && this.count - this.weaponsCount > 150) 
        {
            this.weaponsCount = this.count;
            this.makeWeaponCrate();
        }
        ++this.count;
    }

    function killEnemy(n)
    {
        if (this.enemies[n].hitByBat) 
        {
            var __reg3 = MyMath.getDistance(0, 0, this.enemies[n].xv, this.enemies[n].yv);
            if (__reg3 < 30) 
            {
                this.score = this.score + 2;
            }
            else if (this.enemies[n].yv < 0) 
            {
                this.score = this.score + 6;
                this.addEffect("sixMC", 0, 0);
            }
            else 
            {
                this.score = this.score + 4;
                this.addEffect("fourMC", 0, 0);
            }
        }
        else 
        {
            ++this.score;
        }
        this.removeEnemy(n);
    }

    function removeEnemy(n)
    {
        this.enemies[n].removeMovieClip();
        delete this.enemies[n];
        if (this.getALength(this.enemies) == 0 && this.enemiesDone == 0) 
        {
            this.cursorFrame = "def";
            this.finishLevel();
        }
        this.updateScore();
    }

    function hitAshes(n)
    {
        this.removeEnemy(n);
        this.ashEnergy = this.ashEnergy - 10;
        if (this.ashEnergy <= 0) 
        {
            delete this.onEnterFrame;
            this.cursorBusy = true;
            this.cursor.changeState("def");
            this.ashesMC.gotoAndPlay(2);
            delete this.parent.skyBar.pause.pause.btn.onRelease;
            if (this.parent.highScore < this.score) 
            {
                this.parent.highScore = this.score;
            }
            this.soundCleanup();
            trace("ashesDead");
        }
        this.showAshEnergy();
    }

    function removedAshes()
    {
        var __reg2 = this.attachMovie("gameOverScreen", "gos", 100);
        if (this.score > 300) 
        {
            __reg2.message.gotoAndStop(2);
        }
        this.cursor.changeState("def");
        SoundManager.fadeSound("bgloop", 0, 30);
    }

    function removeObject(ob)
    {
        delete this.objectsA[ob.num];
        ob.removeMovieClip();
    }

    function addFlyingEnemy(m)
    {
        this.flyingEnemies[m.num] = m;
    }

    function removeFlyingEnemy(m)
    {
        delete this.flyingEnemies[m.num];
    }

    function pickUp()
    {
        this.cursorFrame = "pickUp";
        this.cursorBusy = true;
        this.cursor.changeState("pickUp");
    }

    function drop(m)
    {
        this.batCount = 4;
        this.addFlyingEnemy(m);
        this.cursorFrame = "def";
        this.cursor.changeState("def");
        this.cursorBusy = false;
    }

    function getALength(a)
    {
        var __reg1 = 0;
        for (var __reg3 in a) 
        {
            ++__reg1;
        }
        return __reg1;
    }

    function selectWeapon(n)
    {
        if (this.paused) 
        {
            return;
        }
        this.cursorBusy = true;
        if ((__reg0 = n) === 0) 
        {
            this.cursorFrame = "teabag";
        }
        else if (__reg0 === 1) 
        {
            this.cursorFrame = "cCream";
        }
        else if (__reg0 === 2) 
        {
            this.cursorFrame = "bitter";
        }
        else if (__reg0 === 3) 
        {
            this.attachMovie("shakespeare", "shakespeare", 1);
            SoundManager.startSound("snore", 0, 1000);
            for (var __reg3 in this.enemies) 
            {
                var __reg2 = this.enemies[__reg3];
                if (__reg2.onLine) 
                {
                    __reg2.doShakespear();
                }
            }
            --this.powerUpsAmounts[3];
            this.refreshWeaponBtns();
            this.weaponNum = 3;
            this.delCount = 150;
            this.cursorBusy = false;
        }
        else if (__reg0 === 4) 
        {
            this.cursorFrame = "taxi";
        }
        else if (__reg0 === 5) 
        {
            this.cursorFrame = "copper";
        }
        else if (__reg0 === 6) 
        {
            this.cursorFrame = "telephone";
        }
        else if (__reg0 === 7) 
        {
            this.cursorFrame = "raincloud";
        }
        this.cursor.changeState(this.cursorFrame);
    }

    function bgClick()
    {
        if (this.paused) 
        {
            return;
        }
        if ((__reg0 = this.cursorFrame) === "teabag") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            var __reg2 = this.spritesMC.getNextHighestDepth();
            var __reg3 = this.spritesMC.attachMovie("teabag", "tbag" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            if (this.ground.hitTest(__reg3._x, __reg3._y, true)) 
            {
                var __reg6 = this.findPathPoint(__reg3._x);
                __reg3._y = this.pointsA[__reg6].y;
            }
            __reg3.init();
            var __reg4 = this.effectsMC.getNextHighestDepth();
            var __reg5 = this.effectsMC.attachMovie("teacup", "sb" + __reg4, __reg4);
            __reg5._x = this.mouseX;
            __reg5._y = this.mouseY;
            --this.powerUpsAmounts[0];
        }
        else if (__reg0 === "cCream") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("clottedCream", "cCream" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            if (this.ground.hitTest(__reg3._x, __reg3._y, true)) 
            {
                __reg6 = this.findPathPoint(__reg3._x);
                __reg3._y = this.pointsA[__reg6].y;
            }
            __reg3.init();
            --this.powerUpsAmounts[1];
        }
        else if (__reg0 === "bitter") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("bitter", "bitter" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            __reg3.init();
            --this.powerUpsAmounts[2];
        }
        else if (__reg0 === "taxi") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("taxi", "taxi" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            if (this.ground.hitTest(__reg3._x, __reg3._y, true)) 
            {
                __reg6 = this.findPathPoint(__reg3._x);
                __reg3._y = this.pointsA[__reg6].y;
            }
            __reg3.init();
            --this.powerUpsAmounts[4];
        }
        else if (__reg0 === "copper") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("Copper", "cop" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            if (this.ground.hitTest(__reg3._x, __reg3._y, true)) 
            {
                __reg6 = this.findPathPoint(__reg3._x);
                __reg3._y = this.pointsA[__reg6].y;
            }
            __reg3.init();
            --this.powerUpsAmounts[5];
        }
        else if (__reg0 === "telephone") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("Phonebox", "pb" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3._y = this.mouseY;
            if (this.ground.hitTest(__reg3._x, __reg3._y, true)) 
            {
                __reg6 = this.findPathPoint(__reg3._x);
                __reg3._y = this.pointsA[__reg6].y;
            }
            __reg3.init();
            --this.powerUpsAmounts[6];
        }
        else if (__reg0 === "raincloud") 
        {
            this.cursorFrame = "def";
            this.cursorBusy = false;
            __reg2 = this.spritesMC.getNextHighestDepth();
            __reg3 = this.spritesMC.attachMovie("Raincloud2", "rain" + __reg2, __reg2);
            this.objectsA[__reg2] = __reg3;
            __reg3._x = this.mouseX;
            __reg3.init();
            --this.powerUpsAmounts[7];
        }
        this.refreshWeaponBtns();
        this.cursor.changeState("def");
    }

    function checkEnemyHit(ob)
    {
        var __reg3 = [];
        for (var __reg5 in this.enemies) 
        {
            var __reg2 = this.enemies[__reg5];
            if (__reg2.onLine && __reg2.hArea.hitTest(ob)) 
            {
                __reg3.push(__reg2);
            }
        }
        return __reg3;
    }

    function checkEnemyHitWide(ob)
    {
        var __reg3 = [];
        for (var __reg5 in this.enemies) 
        {
            var __reg2 = this.enemies[__reg5];
            if (__reg2.onLine && __reg2.hitTest(ob)) 
            {
                __reg3.push(__reg2);
            }
        }
        return __reg3;
    }

    function checkFlyingEnemyHit(ob)
    {
        var __reg3 = [];
        for (var __reg5 in this.flyingEnemies) 
        {
            var __reg2 = this.flyingEnemies[__reg5];
            if (__reg2.hArea.hitTest(ob)) 
            {
                __reg3.push(__reg2);
            }
        }
        return __reg3;
    }

    function findPathPoint(tx)
    {
        var __reg4 = 1000;
        var __reg5 = undefined;
        var __reg2 = 0;
        while (__reg2 < this.pointsA.length) 
        {
            var __reg3 = Math.abs(tx - this.pointsA[__reg2].x);
            if (__reg3 < __reg4) 
            {
                __reg5 = __reg2;
                __reg4 = __reg3;
            }
            ++__reg2;
        }
        return __reg5;
    }

    function addStarBurst(tx, ty)
    {
        var __reg2 = this.effectsMC.getNextHighestDepth();
        var __reg3 = this.effectsMC.attachMovie("starBurst", "sb" + __reg2, __reg2);
        __reg3._x = tx;
        __reg3._y = ty;
    }

    function addEffect(str, tx, ty)
    {
        var __reg3 = this.effectsMC.getNextHighestDepth();
        var __reg2 = this.effectsMC.attachMovie(str, "ef" + __reg3, __reg3);
        __reg2._x = tx;
        __reg2._y = ty;
        trace("sb:" + __reg2);
    }

    function showAshEnergy()
    {
        this.ashes.gotoAndStop(100 - Math.floor(100 / this.maxAshEnergy * this.ashEnergy));
    }

    function updateScore()
    {
        var __reg2 = this.score;
        if (__reg2 > 9999) 
        {
            __reg2 = 9999;
        }
        this.theInterface.num3.setToNum(this.score % 10);
        if (__reg2 > 9) 
        {
            this.theInterface.num2.setToNum((__reg2 % 100).toString().charAt(0));
        }
        else 
        {
            this.theInterface.num2.setToNum(0);
        }
        if (__reg2 > 99) 
        {
            this.theInterface.num1.setToNum((__reg2 % 1000).toString().charAt(0));
        }
        else 
        {
            this.theInterface.num1.setToNum(0);
        }
        if (__reg2 > 999) 
        {
            this.theInterface.num0.setToNum(__reg2.toString().charAt(0));
            return;
        }
        this.theInterface.num0.setToNum(0);
    }

    function hitSound()
    {
        trace("hitSounds.length:" + this.hitSounds.length);
        var __reg2 = Math.floor(Math.random() * this.hitSounds.length);
        while (this.isInArray(this.prevSound, __reg2)) 
        {
            trace("OldsNum:" + __reg2);
            __reg2 = Math.floor(Math.random() * this.hitSounds.length);
        }
        this.prevSound.shift();
        this.prevSound.push(__reg2);
        trace("newsNum:" + __reg2);
        SoundManager.startSound(this.hitSounds[__reg2]);
    }

    function hitOw()
    {
        trace("hitSounds.length:" + this.hitSounds.length);
        SoundManager.startSound("ow" + Math.floor(Math.random() * 6));
    }

    function isInArray(arr, val)
    {
        var __reg2 = false;
        for (var __reg4 in arr) 
        {
            if (arr[__reg4] == val) 
            {
                __reg2 = true;
            }
        }
        return __reg2;
    }

}


}